package order

import (
	"context"
	"group-buy/group_buy/internal/models"

	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CapitalRecordLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCapitalRecordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CapitalRecordLogic {
	return &CapitalRecordLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CapitalRecordLogic) CapitalRecord(req *types.CapitalRecordReq) (resp *types.CapitalRecordRsp, err error) {
	data := make([]*models.OrderDivisionDetail, 0)
	sql := l.svcCtx.Engine.Table("order_division").Join("LEFT", "order", "order_division.order_id=order.id")
	sql = sql.Join("LEFT", "member as dealer", "dealer.id=order.dealer_id")
	sql = sql.Join("LEFT", "member as invite", "invite.id=order.invite_id")
	sql = sql.Join("LEFT", "business as business", "business.id=order.business_id")
	sql = sql.Join("LEFT", "member as business_user", "business_user.id=business.user_id")

	sql = sql.Where("div_status=2")

	// listSql := sql
	listSql := l.svcCtx.Engine.Table("order_division").Join("LEFT", "order", "order_division.order_id=order.id")
	listSql = listSql.Join("LEFT", "member as dealer", "dealer.id=order.dealer_id")
	listSql = listSql.Join("LEFT", "member as invite", "invite.id=order.invite_id")
	listSql = listSql.Join("LEFT", "business as business", "business.id=order.business_id")
	listSql = listSql.Join("LEFT", "member as business_user", "business_user.id=business.user_id")

	listSql = listSql.Where("div_status=2")
	if req.UserId > 0 {
		sql = sql.And("order_division.div_user_id=?", req.UserId)
		listSql = listSql.And("order_division.div_user_id=?", req.UserId)
	}
	if req.OrderId > 0 {
		sql = sql.And("order_division.order_id=?", req.OrderId)
		listSql = listSql.And("order_division.order_id=?", req.OrderId)
	}
	if req.BusinessId > 0 {
		sql = sql.And("order_division.business_id=?", req.BusinessId)
		listSql = listSql.And("order_division.business_id=?", req.BusinessId)
	}
	if req.DealerId > 0 {
		sql = sql.And("order.dealer_id=?", req.DealerId)
		listSql = listSql.And("order.dealer_id=?", req.DealerId)
	}
	if req.ProductId > 0 {
		sql = sql.And("order.product_id=?", req.ProductId)
		listSql = listSql.And("order.product_id=?", req.ProductId)
	}
	if len(req.Date) > 0 {
		sql = sql.And("order.pay_time like ?", req.Date+"%")
		listSql = listSql.And("order.pay_time like ?", req.Date+"%")
	}
	amount, err := sql.Sum(&models.OrderDivision{}, "div_amount")
	if err != nil {
		return nil, err
	}

	count, err := listSql.Desc("order_division.id").Limit(req.Size, (req.Page-1)*req.Size).FindAndCount(&data)
	if err != nil {
		return nil, err
	}
	list := make([]*types.OrderDivision, len(data), len(data))
	for i, _ := range data {
		list[i] = &types.OrderDivision{
			Id:               data[i].OrderDivision.Id,
			OrderId:          data[i].OrderDivision.OrderId,
			OrderCode:        data[i].OrderDivision.OrderCode,
			DivUserId:        data[i].OrderDivision.DivUserId,
			DivAmount:        data[i].OrderDivision.DivAmount,
			DivTime:          data[i].OrderDivision.DivTime,
			ProductId:        data[i].OrderDivision.ProductId,
			ProductName:      data[i].OrderDivision.ProductName,
			BusinessId:       data[i].OrderDivision.BusinessId,
			DivStatus:        data[i].DivStatus,
			PayUserId:        data[i].PayUserId,
			PayUser:          data[i].PayUser,
			DealerId:         data[i].Dealer.Id,
			DealerName:       data[i].Dealer.Name,
			BusinessName:     data[i].Business.Name,
			BusinessUserId:   data[i].Business.UserId,
			BusinessUserName: data[i].BusinessUser.Name,
			InviteId:         data[i].InviteUser.Id,
			InviteName:       data[i].InviteUser.Name,
		}
	}

	return &types.CapitalRecordRsp{
		List:      list,
		Count:     count,
		SumAmount: amount,
	}, nil
}
